package com.example.Windturbine.Service.Impl;

import com.example.Windturbine.Calculation.TreeDOMethod1.ProductRoot1;
import com.example.Windturbine.Controller.ViewObject.ResultGraphVO.ResultNode;
import com.example.Windturbine.Controller.ViewObject.ResultVO.ListResultVO;
import com.example.Windturbine.Controller.ViewObject.ResultVO.ProductGraphVO;
import com.example.Windturbine.Dao.LCAResultDOMapper;
import com.example.Windturbine.Dao.ProductDOMapper;
import com.example.Windturbine.Dao.ProductResultDOMapper;
import com.example.Windturbine.DataObject.LCAResultDO;
import com.example.Windturbine.DataObject.ProductDO;
import com.example.Windturbine.DataObject.ProductResultDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Service.ProductResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author 丁永新
 * @Date 2021/12/22
 */
@Service
public class ProductResultServiceImpl implements ProductResultService {

    @Resource
    ProductResultDOMapper productResultDOMapper;

    @Resource
    LCAResultDOMapper lcaResultDOMapper;

    @Resource
    ProductDOMapper productDOMapper;


    @Override
    public ListResultVO getResult(Integer cal_id, Integer product_id) throws BusinessException {
        ProductResultDO productResultDO = productResultDOMapper.selectByCaliddAndProductid(cal_id, product_id);
        ProductDO productDO=productDOMapper.selectByPrimaryKey(product_id);

        if(productResultDO==null||productDO==null){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取产品的计算结果时发生错误！");
        }
        try {
            Integer id1 = productResultDO.getRawmaterialMiningStage();
            Integer id2 = productResultDO.getPartTransportationStage();
            Integer id3 = productResultDO.getManufacturingStage();
            Integer id4 = productResultDO.getProductTransportationStage();
            Integer id5 = productResultDO.getProductUseStage();
            Integer id6 = productResultDO.getRecoveryStage();

            ListResultVO result = new ListResultVO();
            LCAResultDO d1 = lcaResultDOMapper.selectByPrimaryKey(id1);
            LCAResultDO d2 = lcaResultDOMapper.selectByPrimaryKey(id2);
            LCAResultDO d3 = lcaResultDOMapper.selectByPrimaryKey(id3);
            LCAResultDO d4 = lcaResultDOMapper.selectByPrimaryKey(id4);
            LCAResultDO d5 = lcaResultDOMapper.selectByPrimaryKey(id5);
            LCAResultDO d6 = lcaResultDOMapper.selectByPrimaryKey(id6);

            //计算PED
            BigDecimal ped = d1.getPed().add(d2.getPed()).add(d3.getPed()).add(d4.getPed()).add(d5.getPed()).add(d6.getPed());
            //计算GWP
            BigDecimal gwp = d1.getGwp().add(d2.getGwp()).add(d3.getGwp()).add(d4.getGwp()).add(d5.getGwp()).add(d6.getGwp());
            //计算AP
            BigDecimal ap = d1.getAp().add(d2.getAp()).add(d3.getAp()).add(d4.getAp()).add(d5.getAp()).add(d6.getAp());
            //计算EP
            BigDecimal ep = d1.getEp().add(d2.getEp()).add(d3.getEp()).add(d4.getEp()).add(d5.getEp()).add(d6.getEp());
            //计算POCP
            BigDecimal pocp = d1.getPocp().add(d2.getPocp()).add(d3.getPocp()).add(d4.getPocp()).add(d5.getPocp()).add(d6.getPocp());
            //计算ODP
            BigDecimal odp = d1.getOdp().add(d2.getOdp()).add(d3.getOdp()).add(d4.getOdp()).add(d5.getOdp()).add(d6.getOdp());
            //计算Green value
            BigDecimal greenvalue = d1.getGreenvalue().add(d2.getGreenvalue()).add(d3.getGreenvalue()).add(d4.getGreenvalue()).add(d5.getGreenvalue()).add(d6.getGreenvalue());


            result.setAp(ap);
            result.setEp(ep);
            result.setGreenvalue(greenvalue);
            result.setGwp(gwp);
            result.setOdp(odp);
            result.setPocp(pocp);
            result.setPed(ped);
            result.setName(productDO.getName());
            result.setType("产品");
            result.setSerial(productDO.getSerial());
            return result;
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"汇集计算结果中发生错误！");
        }
    }

    @Override
    public ProductGraphVO getGraphResult(Integer cal_id, Integer product_id) throws BusinessException {
        try {
            ProductResultDO productResultDO = productResultDOMapper.selectByCaliddAndProductid(cal_id, product_id);
            if(productResultDO==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取产品的计算结果时发生错误！");
            }

            Integer id1 = productResultDO.getRawmaterialMiningStage();
            Integer id2 = productResultDO.getPartTransportationStage();
            Integer id3 = productResultDO.getManufacturingStage();
            Integer id4 = productResultDO.getProductTransportationStage();
            Integer id5 = productResultDO.getProductUseStage();
            Integer id6 = productResultDO.getRecoveryStage();

            ListResultVO result = new ListResultVO();
            LCAResultDO d1 = lcaResultDOMapper.selectByPrimaryKey(id1);
            LCAResultDO d2 = lcaResultDOMapper.selectByPrimaryKey(id2);
            LCAResultDO d3 = lcaResultDOMapper.selectByPrimaryKey(id3);
            LCAResultDO d4 = lcaResultDOMapper.selectByPrimaryKey(id4);
            LCAResultDO d5 = lcaResultDOMapper.selectByPrimaryKey(id5);
            LCAResultDO d6 = lcaResultDOMapper.selectByPrimaryKey(id6);

            ProductGraphVO productGraphVO = new ProductGraphVO();
            productGraphVO.setRaw(d1);
            productGraphVO.setRaw_transport(d2);
            productGraphVO.setManufacture(d3);
            productGraphVO.setPro_transport(d4);
            productGraphVO.setPro_use(d5);
            productGraphVO.setPro_recycle(d6);

            return productGraphVO;
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询产品图形结果时！出现错误！");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public void deleteById(Integer id) throws BusinessException {
        try {
            ProductResultDO productResultDO = productResultDOMapper.selectByPrimaryKey(id);
            if(productResultDO==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"删除时发生错误！");
            }
            lcaResultDOMapper.deleteByPrimaryKey(productResultDO.getManufacturingStage());
            lcaResultDOMapper.deleteByPrimaryKey(productResultDO.getPartTransportationStage());
            lcaResultDOMapper.deleteByPrimaryKey(productResultDO.getProductTransportationStage());
            lcaResultDOMapper.deleteByPrimaryKey(productResultDO.getProductUseStage());
            lcaResultDOMapper.deleteByPrimaryKey(productResultDO.getRawmaterialMiningStage());
            lcaResultDOMapper.deleteByPrimaryKey(productResultDO.getRecoveryStage());
            productResultDOMapper.deleteByPrimaryKey(id);
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"删除信息失败！");
        }
    }

    @Override
    public ResultNode getMetaResult(Integer cal_id, Integer product_id) throws BusinessException {
        ProductResultDO productResultDO = productResultDOMapper.selectByCaliddAndProductid(cal_id, product_id);
        ProductDO productDO=productDOMapper.selectByPrimaryKey(product_id);

        if(productResultDO==null||productDO==null){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取产品的计算结果时发生错误！");
        }
        try {
            Integer id1 = productResultDO.getRawmaterialMiningStage();
            Integer id2 = productResultDO.getPartTransportationStage();
            Integer id3 = productResultDO.getManufacturingStage();
            Integer id4 = productResultDO.getProductTransportationStage();
            Integer id5 = productResultDO.getProductUseStage();
            Integer id6 = productResultDO.getRecoveryStage();

            ResultNode result = new ResultNode();
            LCAResultDO d1 = lcaResultDOMapper.selectByPrimaryKey(id1);
            LCAResultDO d2 = lcaResultDOMapper.selectByPrimaryKey(id2);
            LCAResultDO d3 = lcaResultDOMapper.selectByPrimaryKey(id3);
            LCAResultDO d4 = lcaResultDOMapper.selectByPrimaryKey(id4);
            LCAResultDO d5 = lcaResultDOMapper.selectByPrimaryKey(id5);
            LCAResultDO d6 = lcaResultDOMapper.selectByPrimaryKey(id6);

            //计算PED
            BigDecimal ped = d1.getPed().add(d2.getPed()).add(d3.getPed()).add(d4.getPed()).add(d5.getPed()).add(d6.getPed());
            //计算GWP
            BigDecimal gwp = d1.getGwp().add(d2.getGwp()).add(d3.getGwp()).add(d4.getGwp()).add(d5.getGwp()).add(d6.getGwp());
            //计算AP
            BigDecimal ap = d1.getAp().add(d2.getAp()).add(d3.getAp()).add(d4.getAp()).add(d5.getAp()).add(d6.getAp());
            //计算EP
            BigDecimal ep = d1.getEp().add(d2.getEp()).add(d3.getEp()).add(d4.getEp()).add(d5.getEp()).add(d6.getEp());
            //计算POCP
            BigDecimal pocp = d1.getPocp().add(d2.getPocp()).add(d3.getPocp()).add(d4.getPocp()).add(d5.getPocp()).add(d6.getPocp());
            //计算ODP
            BigDecimal odp = d1.getOdp().add(d2.getOdp()).add(d3.getOdp()).add(d4.getOdp()).add(d5.getOdp()).add(d6.getOdp());
            //计算Green value
            BigDecimal greenvalue = d1.getGreenvalue().add(d2.getGreenvalue()).add(d3.getGreenvalue()).add(d4.getGreenvalue()).add(d5.getGreenvalue()).add(d6.getGreenvalue());

            result.setMaterial("组装件");
            result.setManu("成品件");
            result.setPercent(10);
            result.setTransport(productDO.getAim());

            result.setAp(ap);
            result.setEp(ep);
            result.setGreenvalue(greenvalue);
            result.setGwp(gwp);
            result.setOdp(odp);
            result.setPocp(pocp);
            result.setPed(ped);
            result.setName(productDO.getName());
            result.setOrderInfo("产品");
            result.setSerialInfo(productDO.getSerial());
            result.setRaw(d1.getGreenvalue());
            result.setRaw_transport(d2.getGreenvalue());
            result.setManufacture(d3.getGreenvalue());
            result.setPro_transport(d4.getGreenvalue());
            result.setPro_use(d5.getGreenvalue());
            result.setRecycle(d6.getGreenvalue());
            return result;
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询产品图形结果时！出现错误！");
        }



    }
}
